package com.itheima.dynamic;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Test01 {
    @Test
    public void selectByIds() throws Exception{
//     1.加载核心配置类
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
//     2.获取SqlSession工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//        3.通过工厂对象获取SqlSession对象
        SqlSession sqlSession = build.openSession();
//        4.获取映射文件的接口对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setId(1);
//        5.执行sql语句
        List<Student> students = mapper.selectCondition(student);
        System.out.println("students = " + students);
//        6.释放资源
        sqlSession.close();
        resourceAsStream.close();
    }

    @Test
    public void selectCondition() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(2);
        List<Student> students = mapper.selectByIds(objects);
        System.out.println("students = " + students);
        sqlSession.close();
        resourceAsStream.close();
    }
}
